package com.atguigu.day20.syn01.exer.e2;

//todo 窗口线程
//todo 同步范围太大 变为单线程 太小 没有互斥效果

public class WindowThread extends Thread {
    //todo 静态成员变量 被所有对象共享
    static int tickets = 200;

    @Override
    public void run() {


        while (true) {
            if (tickets <= 0) {
                break;
            }
            //卖票
          //  WindowThread.sellTicket();
            sellTicket();

        }
    }
        //this:
    //WindowThread.class
    public static synchronized void sellTicket() {
        //防止出现负票
        if(tickets<=0){
            return;
        }
        tickets--;
        try {
            Thread.sleep(20);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }

        System.out.println(Thread.currentThread().getName() + "卖了一张票,还有[" + tickets + "]张票");
    }
}
